package java6681.dao;

import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

/**
 * @author Junichiro
 * @description 数据库连接的工具类
 * @date 2021-05-23 13:01
 **/

public class DBUtil6681 {
    private static String url;
    private static String user;
    private static String password;
    private static String driver;

    /*文件的读取，只需要读取依次即可拿到这些值。使用静态代码块*/
    static {
        /*读取资源文件，获取值*/
        try {
            /*1.创建Properties集合类*/
            Properties pro = new Properties();

            /*获取src路径下的文件的方式 ---> ClassLoader 类加载器*/
            ClassLoader classLoader = DBUtil6681.class.getClassLoader();
            //没设置正确路径会报错 NoClassDefFoundError: Could not initialize class java6681.dao.DBUtil6681
            URL res = classLoader.getResource("java6681/config/jdbc.properties");
            String path = res.getPath();
//            System.out.println(path);

//            /*2.加载文件*/
//            pro.load(new FileReader("src/jdbc.properties"));
            pro.load(new FileReader(path));

            /*3.获取数据，赋值*/
            url = pro.getProperty("url");
            user = pro.getProperty("user");
            password = pro.getProperty("password");
            driver = pro.getProperty("driver");
            /*4.注册驱动*/
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    /**
     * 功能：获取连接
     *
     * @param
     * @return 连接对象
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

    /**
     * 功能：释放资源
     *
     * @param stmt
     * @param conn
     */
    public static void close(Statement stmt, Connection conn) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 功能：释放资源
     *
     * @param rs
     * @param stmt
     * @param conn
     */
    public static void close(ResultSet rs, Statement stmt, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
